<template>
  <el-card>
    <el-row :gutter="20" class="header">
      <el-col :span="7">
        <el-input  placeholder="请输入用户昵称..." clearable />
      </el-col>
      <el-button type="primary" @click="initUserList">搜索</el-button>
    </el-row>
    <el-table :data="tableData" stripe style="width: 100%;text-align: center">
      <template v-if="isStudent">
        <el-table-column prop="homeworkid" label="ID" width="80" />
        <el-table-column prop="name" label="用户昵称" />
        <el-table-column label="上交作业情况">
          <template slot-scope="scope">
            <span v-if="scope.row.submitcount == 1">已上交</span>
            <span v-else>未上交</span>
          </template>
        </el-table-column>
        <el-table-column prop="text" label="作业内容" />
        <el-table-column prop="score" label="分数等级" />
        <el-table-column prop="submittime" label="上交时间" />
      </template>
      <template v-else-if="isTeacher">
        <el-table-column prop="title" label="作业题目" />
        <el-table-column prop="banji" label="班级" />
        <el-table-column label="上交情况">
          <template slot-scope="scope">
            <span>{{ scope.row.submitcount
            }}人 已完成</span>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="发布时间" />
        <el-table-column label="操作" width="200">
          <template v-slot="scope">
            <el-button v-if="isTeacher" type="primary" size="small" icon="el-icon-edit-outline" @click="review(scope.row)">阅卷</el-button>
            <el-button type="danger" size="small" icon="el-icon-refresh-left" @click="deleted(scope.row)">打回</el-button>
          </template>
        </el-table-column>
      </template>
    </el-table>

    <!-- <el-pagination
      :current-page="queryForm.pageNum"
      :page-sizes="[10, 20, 30, 40,50]"
      :page-size="queryForm.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    /> -->
    <el-dialog
      title="修改信息"
      :visible.sync="centerDialogVisible"
      width="30%"
      center
    >
      <div>
        <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm">
          <el-form-item label="分数" prop="score">
            <el-input v-model="ruleForm.score"  />
          </el-form-item>
          <!-- <el-form-item label="学号" prop="studentId">
            <el-input  />
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input  />
          </el-form-item> -->
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
      </span>
    </el-dialog>
  </el-card>
</template>


<script>
import { getList, getTeacherHomeWorkList, deleted, update, addScore } from '@/api/homework'
export default { 
  data() {
    return {
      queryForm: {
        pageSize: 10,
        pageNum: 1,
        name: ''
      },
      tableData: [],
      total: 0,
      centerDialogVisible: false,
      ruleForm: {},
      rules: {
        score: [
          { required: true, message: '请输入分数', trigger: 'blur' },
        ],
        studentId: [
          { required: true, message: '请输入学号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
        ]

      },
      userInfo: {}
    }
  },
  created() {
    this.initUserList()
  },
  computed: {
    isAdmin() {
      const userInfo = JSON.parse(localStorage.getItem('userInfo'))
      return userInfo && userInfo.studentId === 'admin' && userInfo.identity === '1'
    },
    isTeacher() {
      const userInfo = JSON.parse(localStorage.getItem('userInfo'))
      return userInfo && userInfo.identity == 1 && userInfo.studentId !== 'admin'
    },
    isStudent() {
      const userInfo = JSON.parse(localStorage.getItem('userInfo'))
      return userInfo && userInfo.identity == 0
    },
    currentUser() {
      return JSON.parse(localStorage.getItem('userInfo')) || {}
    }
  },
  methods: {
    initUserList() {
      this.tableData = []
      this.total = 0
      const userInfo = this.currentUser
      if (this.isAdmin) {
        // 管理员查所有学生的作业情况
        getList().then(res => {
          if (res.code == 200) {
            this.tableData = res.data
          }
        })
      } else if (this.isTeacher) {
        // 老师查自己发布的作业
        getTeacherHomeWorkList({ id: userInfo.id }).then(res => {
          if (res.code == 200) {
            this.tableData = res.data
          }
        })
      } else if (this.isStudent) {
        // 学生查自己的所有作业
        getList({ id: userInfo.id }).then(res => {
          if (res.code == 200) {
            this.tableData = res.data
          }
        })
      }
    },
    review(item) {
      this.centerDialogVisible = true
      this.ruleForm = item
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          addScore({
            id: this.ruleForm.homeworkid,
            studentId: this.ruleForm.studentId,
            score: this.ruleForm.score
          }).then(res => {
            this.$message({
              type: 'success',
              message: '评分成功!'
            })
            this.centerDialogVisible = false
            this.initUserList()
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          update({
            id: this.ruleForm.homeworkid ,
            studentId :this.ruleForm.studentId,
            score:this.ruleForm.score
          }).then(res => {
            this.$message({
              type: 'success',
              message: '修改成功!'
            })
            this.centerDialogVisible = false
            this.initUserList()
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    deleted(item) {
      console.log(item);
      this.$confirm('此操作将打回该作业, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleted({ id: item.homeworkid , studentId : item.studentId }).then(res => {
            this.$message({
              type: 'success',
              message: '打回成功!'
            })
            this.initUserList()
        })
      })
    },
    update(item) {
      this.centerDialogVisible = true
      this.ruleForm = item
    },
  }
}
</script>